<?php
	//<script src="http://www.localhost/Views/AdViewer.php?uiId=ad&cacheBuster=1&containerId=1"></script>
	require_once '../Controllers/ContainerController.php';
	$c = new ContainerController();
	//
	function init()
	{
		if(!empty($_GET))
		{
			wrapJs();
				renderBaseScript();
				renderHelperScript();
				renderAdScript();
				renderAd();
			wrapJs(true);
		}
	}
	function renderAd()
	{
		$a = $_GET['containerId'];
		$adType = 'htmlText';
		//
		/*$cntr = new Container();
		$cntr->id = $a;
		renderFlash($c->read($cntr));
		renderNoScript($c->getDefaultImage($a));*/
		switch($adType)
		{
			case 'htmlText':
				renderHtmlTextScript('<b>hello</b> world');
			break;
			case 'image':
				renderDefaultImageScript('http://img.labnol.org/files/google-logo.jpg');
			break;
		}
	}
	function renderBaseScript()
	{
		//http://ejohn.org/blog/simple-javascript-inheritance
		$s = '(function(){
			  var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
			  this.Class = function(){};
			  Class.extend = function(prop) {
				var _super = this.prototype;
				initializing = true;
				var prototype = new this();
				initializing = false;
				for (var name in prop) {
				  prototype[name] = typeof prop[name] == "function" && 
					typeof _super[name] == "function" && fnTest.test(prop[name]) ?
					(function(name, fn){
					  return function() {
						var tmp = this._super;
						this._super = _super[name];
						var ret = fn.apply(this, arguments);        
						this._super = tmp;
						return ret;
					  };
					})(name, prop[name]) :
					prop[name];
				}
				function Class() {
				
				  if ( !initializing && this.init )
					this.init.apply(this, arguments);
				}
				Class.prototype = prototype;
				Class.prototype.constructor = Class;
				Class.extend = arguments.callee;
				return Class;
			  };
			})();';
			echo $s;
	}
	function wrapJs($close = false)
	{
		$s = $close ? '/' : '';
		echo 'document.write("<' . $s . 'scr" + "ipt>");' . "\n";
	}
	function renderHelperScript()
	{
		echo 'var Helper = Class.extend
				(
					{
						getFlashAd:function(movieName)
						{
							if (navigator.appName.indexOf("Microsoft") != -1) 
							{
								return window[movieName];
							} 
							else 
							{
								return document[movieName];
							}
						},
						getSandBoxRoot:function()
						{
							//BUG:
							var tmp = document;
							var pos;do
							{
								if(tmp.parent)
								{
									pos = tmp.parent;
									tmp = pos;
								}
							}
							while(tmp != pos);
							return pos;
						}
					}
				);';
	}
	function renderAdScript()
	{
		echo 'var Ad = Class.extend
				(
					{
						init:function()
						{
							this.uiContainerId = ' . $_GET['uiContainerId'] . ';
							this.PUBLISHER_PATH = "";
							//this.h = new Helper();
						},
						//For non-rich banners.
						countMetric:function(type)
						{
							var i = this.o.createElement("image");
							var s = this.PUBLISHER_PATH + "?containerId=n&action=" + type;
							switch(type)
							{
								case "impression":
									//Do nothing.
								break;
								case "clickThrough":
									s += "&id=0";
								break;
							}
							i.src = s;
							this.d.appendChild(i);
							this.d.removeChild(i);
						},
						//Override.
						onRender:function(){},
						onRemove:function(){}
					}
				);';
	}
	function renderHtmlTextScript($htmlText)
	{
		echo 'var HtmlTextAd = Ad.extend
				(
					{
						init:function()
						{
							this._super();
							this.render();
						},
						render:function()
						{
							var o = document.createElement("div");
							o.innerHTML = "' . $htmlText . '";
							this.uiContainerId.appendChild(o);
						}
					}
				);
				new HtmlTextAd();';
	}
	function renderStandardScript($ad)
	{
		if($ad->isHeavy)
		{
			//Loads a lighter version Standard Banner
			//while the heavy Flash Banner is loading.
		}
		else
		{
			//Uses Standard Banner.
		}
	}
	function renderWallpaperScript($ad)
	{
		//Shows a standard Flash and replaces the page's entire bg based on that ad.
	}
	function renderWindowScript($ad)
	{
		//Shows a popup window with any contents.
	}
	function renderExpandableScript($ad)
	{
		//Render one standard resizable Flash.
		//Resize the ad div according to the standard Flash.
	}
	function renderMultipleExpandableScript($ad)
	{
		//Render one standard Flash.
		//Render all panels.
	}
	//
	function renderInterstitialScript($ad)
	{
		//Grab the real URL and relocate to a page with only the ad.
		//Skipping the ad, goes to the real URL.
	}
	function renderFloatingScript($ad)
	{
		//With reminder.
		//Coords.  Also sticks to the walls.
	}
	function renderSlideScript($ad)
	{
		//Pushes the entire content to any direction, showing only the ad.
	}
	function renderVastScript($ad)
	{
		switch($ad->version)
		{
			//
		}
	}
	function renderFlashMarkup($ad, $swfFile)
	{
		$w = $ad->width;
		$h = $ad->height;
		$f = $swfFile;
		echo '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="' . $w . '" height="' . $h . '" id="' . $ad->Id . '" align="middle">';
				'<param name="movie" value="' . $f . '" />';
				'<param name="quality" value="high" />';
				'<param name="bgcolor" value="#ffffff" />';
				'<param name="play" value="true" />';
				'<param name="loop" value="true" />';
				'<param name="wmode" value="window" />';
				'<param name="scale" value="showall" />';
				'<param name="menu" value="false" />';
				'<param name="devicefont" value="false" />';
				'<param name="salign" value="" />';
				'<param name="allowScriptAccess" value="always" />';
				'<!--[if !IE]>-->';
				'<object type="application/x-shockwave-flash" data="' . $f . '" width="' . $w . '" height="' . $h . '">';
					'<param name="movie" value="' . $f . '" />';
					'<param name="quality" value="high" />';
					'<param name="bgcolor" value="#ffffff" />';
					'<param name="play" value="true" />';
					'<param name="loop" value="true" />';
					'<param name="wmode" value="window" />';
					'<param name="scale" value="showall" />';
					'<param name="menu" value="false" />';
					'<param name="devicefont" value="false" />';
					'<param name="salign" value="" />';
					'<param name="allowScriptAccess" value="always" />';
				'<!--<![endif]-->';
					'<a href="http://www.adobe.com/go/getflash">';
						'<img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" />';
					'</a>';
				'<!--[if !IE]>-->';
				'</object>';
				'<!--<![endif]-->';
			'</object>';
	}
	function renderFlash($container)
	{
		$s = '';
		switch($container->type)
		{
			case ContainerType::INTERNAL:
				switch($container->adType)
				{
					case AdType::HTML_TEXT:
						$s = getHtmlText($ad);
					break;
					case AdType::IMAGE:
						$s = getImage($ad);
					break;
					case AdType::Standard:
						$s = getStandard($ad);
					break;
					case AdType::EXPANDABLE:
						$s = getExpandable();
					break;
					case AdType::MULTIPLE_EXPANDABLE:
						$s = getMultipleExpandable($ad);
					break;
					case AdType::SLIDE:
						$s = getSlide($ad);
					break;
					case AdType::INTERSTITIAL:
						$s = getInterstitial($ad);
					break;
				}
			break;
			case ContainerType::EXTERNAL:
				switch($$container->adType)
				{
					case AdType::WALLPAPER:
						$s = getWallpaper($ad);
					break;
					case AdType::FLOATING:
						$s = getFloating($ad);
					break;
					case AdType::WINDOW:
						$s = getWindow($ad);
					break;
				}
			break;
			case ContainerType::VAST:
				$s = getVast($ad);
			break;
			case ContainerType::MOBILE:
				$s = getMobile($ad);
			break;
		}
		echo $s;
	}
	function renderDefaultImageScript($url)
	{
		//TODO:  onclick and impression tracking.
		echo 'var ImageAd = Ad.extend
			(
				{
					init:function()
					{
						this._super();
						//
						var o = document.createElement("img");
						var ref = this;
						o.onclick = function(){ref.countMetric("clickThrough");};
						o.src = "' . $url . '";
						this.uiContainerId.appendChild(o);
					}
				}
			);
			new ImageAd();';
	}
	init();
?>